package com.killer.task;

import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import com.killer.pojo.entity.BOrder;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class ExportTask implements Runnable{

    private List<BOrder> list;


    private static final String savePath = "d://export/";

    private CountDownLatch countDownLatch;

    private Integer i;

    public ExportTask(List<BOrder> data,CountDownLatch countDownLatch,Integer i){
        this.list = data;
        this.countDownLatch = countDownLatch;
        this.i = i;
    }

    @SneakyThrows
    @Override
    public void run() {
        Workbook workbook = new XSSFWorkbook();
        try(FileOutputStream fileOutputStream = new FileOutputStream(savePath + "订单详情表（"+i + 1+").xlsx")) {
            Sheet sheet = workbook.createSheet("sheet1");
            int rowNum = 0;
            for(BOrder item : list){
                Row row = sheet.createRow(rowNum++);
                int cellNum = 0;
                Field[] fields = item.getClass().getDeclaredFields();
                for(Field field : fields){
                    field.setAccessible(true);
                    Object fieldValue = field.get(item);
                    Cell cell = row.createCell(cellNum++);
                    cell.setCellValue(String.valueOf(fieldValue));
                }
            }
            workbook.write(fileOutputStream);
            countDownLatch.countDown();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
        }

    }
}
